package com.raccoonagents.tools.cascet2d.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.raccoonagents.tools.cascet2d.interfaces.IResourceLoaderListener;
import com.raccoonagents.tools.cascet2d.model.AbstractResource;
import com.raccoonagents.tools.cascet2d.model.ImageResource;
import com.raccoonagents.tools.cascet2d.model.exceptions.UnavailableException;
/**
 * utility class for loading resources.
 * @author jabi
 *
 */
public class ResourceLoader {
	private final List<ImageResource> imageResources = new ArrayList<ImageResource>();
	private static ResourceLoader instance;
	private final List<IResourceLoaderListener> listeners = new ArrayList<IResourceLoaderListener>();
	
		
	/**
	 * 
	 * @return instance
	 */
	public static ResourceLoader getInstange(){
		if(instance == null){
			instance = new ResourceLoader();
		}
		return instance;
	}
	/**
	 * adds a listener
	 * @param listener
	 */
	public void addLoaderListener(final IResourceLoaderListener listener){
		listeners.add(listener);
	}
	/**
	 * removes a listener
	 * @param listener
	 */
	public void removeLoaderListener(final IResourceLoaderListener listener){
		listeners.remove(listener);
	}
	/**
	 * removes a resource
	 * @param resource
	 */
	public void removeResource(final AbstractResource resource){
		if(resource instanceof ImageResource){
			imageResources.remove(resource);
		}else{
			FWLogger.warning("cannot delete resource(unimplemented!)");
		}
	}
	/**
	 * 
	 * @param file
	 * @return
	 * @throws UnavailableException
	 */
	public AbstractResource getLoadedResource(final File file) throws UnavailableException{
		for(final AbstractResource r : imageResources){
			if(r.getHandle().getAbsolutePath().equals(file.getAbsolutePath())){
				return r;
			}
		}
		throw new UnavailableException("resource " + file.getAbsolutePath()+ " is unavailable");
	}
	/**
	 * 
	 * @return the list of imageresources from memory
	 */
	public List<ImageResource> getImageResources() {
		return imageResources;
	}
	/**
	 * 
	 * @param path
	 * @throws IOException
	 */
	public ImageResource loadImageResource(final String path) throws  IOException{
		final File file = new File(path);
		if(!file.exists()){
			throw new IOException("file does not exist : " + path);
		}
		final ImageResource resource = new ImageResource(file);
		try{
			resource.load();			
			imageResources.add(resource);
			
		}catch(final IOException e){
			FWLogger.warning(e.getMessage());
		}

		for(final IResourceLoaderListener listener : listeners){
			listener.resourceLoaded(resource);
		}
		
		return resource;
	}
	/**
	 * 
	 * @param path
	 * @throws IOException
	 */
	public void loadImageResourcesFromFolder(final String path) throws IOException{
		final File file = new File(path);
		
		if(!file.exists()){
			throw new IOException("directory does not exist : " + path);
		}
		if(!file.isDirectory()){
			throw new IOException("not a directory : " + path);			
		}
		
		for(final File f : file.listFiles()){
			loadImageResource(f.getAbsolutePath());
		}
	}
	
}
